package com.li.o2oSystem.web.wechat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.li.o2oSystem.dto.UserAccessToken;
import com.li.o2oSystem.dto.WechatAuthExecution;
import com.li.o2oSystem.dto.WechatUser;
import com.li.o2oSystem.entity.PersonInfo;
import com.li.o2oSystem.entity.WeChatAuth;
import com.li.o2oSystem.enums.WechatAuthStateEnum;
import com.li.o2oSystem.service.PersonInfoService;
import com.li.o2oSystem.service.WechatAuthService;
import com.li.o2oSystem.util.weixin.WechatUtil;
/**
 * 获取关注公众号之后的微信用户信息的接口，在微信浏览器中访问
 * https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1f8783ad7c3873b1&redirect_uri=http://www.lisheng.store/o2oSystem/wechatlogin/logincheck&role_type=1&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect
 * @author Administrator
 */
@Controller
@RequestMapping("wechatlogin")
public class WechatLoginController {
	private static Logger log = LoggerFactory.getLogger(WechatLoginController.class);
	private static final String FRONTEND="1";//用户标识
	private static final String SHOPEND = "2";//店家标识
	
	@Autowired
	private PersonInfoService personInfoService; 
	@Autowired
	private WechatAuthService wechatAuthService;
	@RequestMapping(value = "/logincheck", method = { RequestMethod.GET }) 
	public String doGet(HttpServletRequest request,HttpServletResponse response){
		log.debug("weixin login get...");
		//获取微信公众号传输过来的code,通过code可获取access_token,进而获取用户的信息
		String code = request.getParameter("code");
		//state用来传递用户自定义信息，方便程序调用
		String roleType = request.getParameter("state");
		WechatUser user = null;
		String openId = null;
		WeChatAuth auth = null;	//微信用户实体
		if(code != null){
			UserAccessToken token;
			try{
				//通过code获取access_token
				token = WechatUtil.getUserAccessToken(code);
				//通过token获取accessToken
				String accessToken = token.getAccessToken();
				//通过token获取openId
				openId = token.getOpenId();
				//通过access_token和openId获取用户昵称等信息
				user = WechatUtil.getUserInfo(accessToken,openId);
				request.getSession().setAttribute("openId", openId);
				
				//从微信中获取微信用户的信息，根据openId
				auth = wechatAuthService.getWechatAuthByOpenId(openId);
				
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		//判断微信用户是否等于空,等于空，则注册
		if(auth == null){
			PersonInfo personInfo = WechatUtil.getPersonInfoFromRequest(user);
			auth = new WeChatAuth();
			auth.setOpenId(openId);
			if(FRONTEND.equals(roleType)){
				personInfo.setUserType(1);//设置用户类型,为1则是用户，进入前端页面
			}
			else{
				personInfo.setUserType(2);//否则设置为2，为店家，进入店铺管理页面
			}
			auth.setPersonInfo(personInfo);
			WechatAuthExecution we = wechatAuthService.register(auth);
			//判断是否注册成功，
			if(we.getState() != WechatAuthStateEnum.SUCCESS.getState()){
				return null;
			}
			//如果创建成功
			else{//根据用户id获取用户信息
				personInfo = personInfoService.getPersonInfoById(auth.getPersonInfo().getUserId());
				request.getSession().setAttribute("user", personInfo);//将用户信息设置进session中
			}
		}
		//判断roleType,
		if(FRONTEND.equals(roleType)){
			return "frontend/index";
		}
		else{
			return "shopadmin/shoplist";
		}
	}
}